<?php
/*
	股东中心
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
set_html('title','我的业绩');
if($_RQ['op']=='main'){
	$basic=model('core/member')->basic($_SESSION['uid']);
	$worker=array_merge($worker,$basic);
	$return['worker']=$worker;
	$return['worker']['role']=pdo_getcolumn('jy_weishop_store_role',array('id'=>$member['roleid']),'name');
	$data=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'workerid'=>$this->workerid);
	$data['total_price']=pdo_getsum('jy_weishop_store_commission',$params,'total_price');
	$data['commission_money']=pdo_getsum('jy_weishop_store_commission',$params,'money');
	$data['area_num']=pdo_count('core_members',array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$_SESSION['uid']));
	$params=array('uniacid'=>$_SESSION['uniacid'],'workerid'=>$this->workerid);
	$order_num=pdo_count('jy_weishop_store_commission',$params);
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$agent_ids=$_SESSION['uid'];
	$params['workerid']=$this->workerid;
	$agent_num=pdo_count('jy_weishop_member',$params);
	$data['agent_num']=$agent_num;
	$data['order_num']=$order_num;
	$data['workernum']=pdo_count('jy_weishop_store_worker',array('uniacid'=>$_SESSION['uniacid']));
	$data['workergroupnum']=pdo_count('jy_weishop_store_role',array('uniacid'=>$_SESSION['uniacid']));
	$return['data']=$data;
	$return['params']=array('type'=>0);
	exi($return);
}
if($_RQ['op']=='getlist'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_jcfuwu','is_team'=>1);
	$ids=pdo_getall('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'workerid'=>$this->workerid),array('mid'),'mid');
	if($ids){
		$ids=array_keys($ids);
		$params['mid']=$ids;
	}else{
		$params['mid']=-1;
	}
	$type=intval($_RQ['type']);
	if($type==1){
		$params['ispay']=1;
	}elseif($type==2){
		$params['status']=array(5,6);
	}else{
		$params['ispay']=0;
	}
	//我的团
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		foreach($list as &$item){
			$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
			$merchs =array();
			$item['feemoney']=0;
			foreach($ordergoods as &$gv){
				$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','marketprice','unit','image','title','merchid'));
				$goodsinfo['total']=$gv['total'];
				$goodsinfo['price']=$gv['price'];
				if($_SESSION['is_tomedia']){
				$goodsinfo['image']=$goodsinfo['image']?tomedia($goodsinfo['image']):'';
				}
				$goodsinfo['weight']=pdo_getcolumn('jy_weishop_goods_dispatch',array('goodsid'=>$gv['goodsid']),'weight');
				if($feemoneyset['isopen']==1){
					$gv['feemoney']=$goodsinfo['marketprice']>0?$goodsinfo['marketprice']-$item['price']:$feemoneyset['feerade']*$item['price']/100;
					$gv['feemoney']=$gv['total']*$gv['feemoney'];
					$item['feemoney'] +=$gv['feemoney'];
					$goodsinfo['feemoney']=price($gv['feemoney']);
				}
				//获取门店
				$stores = array();
				$goodsinfo['storeids'] = json_decode($goodsinfo['storeids'],true);
				if ($goodsinfo['isverify'] == 1) {
					if (empty($goodsinfo['storeids'])) {
						$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3)));
					}else {
						foreach($goodsinfo['storeids'] as $sv){
							$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'type'=>array(2,3),'id'=>$sv['id']));
						}
					}
				}elseif($shopset['openstore'] ==1){
					if (empty($goodsinfo['storeids'])) {
						$stores = pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
					}else {
						foreach($goodsinfo['storeids'] as $sv){
							$stores[]= pdo_get('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'id'=>$sv['id']));
						}
					}
				}
				$goodsinfo['store']=$stores;
				//商品规格
				if($gv['optionid']){
					$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
					$specs=explode("_",$specs);
					foreach($specs as &$spec){
						$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
					}
					$goodsinfo['selespename']=implode("，",$specs);
					if($item['api_data'] && $item['api_pluginname']=='jy_jcfuwu'){
						$item['api_data']=json_decode($item['api_data'],true);
						$item['api_data']['starttime']=date('Y-m-d',time()+2*24*3600);
						if($item['api_data']['numtype']==0){
							$num=$item['api_data']['num'];
						}elseif($item['api_data']['numtype']==1){
							$num=$item['api_data']['num']*30;
						}elseif($item['api_data']['numtype']==2){
							$num=$item['api_data']['num']*365;
						}
						$enddays=2+$num;
						$item['api_data']['endtime']=date('Y-m-d',time()+$enddays*24*3600);
						$item['is_zu']=1;
					}
				}
				//获取商户
				if($goodsinfo['merchid']){
					$merchinfo = pdo_get('jy_merch_user',array('id'=>$goodsinfo['merchid'],'uniacid'=>$_SESSION['uniacid']));
					$merchs[$goodsinfo['merchid']] = $merchinfo;
					$merchs[$goodsinfo['merchid']]['goods'][] = $goodsinfo;
				}else{
					$merchs[0]['goods'][] = $goodsinfo;
				}
			}
			$item['feemoney']=price($item['feemoney']);
			//获取地址
			$item['address']=json_decode($item['address'],true);
			if($item['address'] && $item['address']['name']){	
				$address=$item['address'];
			}else{
				$address=$item['addressid'];
			}
			$item['address']=model('core/member')->get_address_str($address);
			$item['pingjiatime'] = pdo_getcolumn('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id'],'commentid'=>0),'createtime');
			if($item['pingjiatime']){
				$comment_changedays=$shopset['comment_changedays']?:0;
				$endpingjiatime = $item['pingjiatime'] + $comment_changedays*24*3600;
				if($endpingjiatime>TIMESTAMP){
					$item['pingjia']=1;//1修改评价；2追加评价
				}else{
					$zuipingjia = pdo_count('jy_weishop_goods_comments',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id'],'commentid >'=>0),'id');
					if($zuipingjia<1){
						$item['pingjia']=2;
					}else{
						$item['pingjia']=3;
					}
				}
			}
			$item['merchs']=$merchs;
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['statustitle']=$this->order_status[$item['status']];
			$item['buytypetitle']=$this->buy_types[$item['buytype']];
			$item['total']=pdo_getsum('jy_weishop_order_goods',array('orderid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'total');
			$item['can_refund']=model('jy_weishop/order/refund')->check_order_rerefund($item['id']);
		}
	}
	$return['list']=$list;
	$return['address_list'] = pdo_getall('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	if($return['address_list']){
		foreach($return['address_list'] as &$add){
			$add['pcd']=json_decode($add['pcd'],true);
		}
	}
	exi($return);
}
?>